package pprof

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"os"
	"runtime/pprof"
)

//可以通过 pprof 对 程序进行调优
func UsePprofCase() {
	for i := 0; i < 1000; i++ {
		var mp = make(map[string]int,i)
		mp[fmt.Sprintf("user_%v",i)] = i
		log.Println(int(i*10*88) * rand.Intn(6000))
		s ,_ := json.MarshalIndent(mp,"","  ")
		fmt.Println(s)
	}

	memf, err := os.Create("mem.prof")
	if err != nil {
		log.Fatalf("%+v", err)
	}
	defer memf.Close()
	//runtime.GC
	if err := pprof.WriteHeapProfile(memf); err != nil {
		log.Fatalf("could not write memory profile: ", err)
	}
	f2, err := os.Create("gorutine.prof")
	if err != nil {
		log.Fatalf("error msg = %+v", err)
	}
	defer f2.Close()

	if gProf := pprof.Lookup("goroutine"); gProf == nil {
		log.Fatalf("could not write goroutine profile: ")
	}else {
		gProf.WriteTo(f2,0)
	}


}
